package com.sunshine.lnuplusweb.utils

import com.google.gson.Gson
import com.google.gson.JsonParser
import com.google.gson.reflect.TypeToken
import com.sunshine.lnuplusweb.callback.LoginCallback
import com.sunshine.lnuplusweb.entities.Lesson
import com.sunshine.lnuplusweb.vo.superlesson.SuperBean
import com.sunshine.lnuplusweb.vo.superlesson.SuperData
import com.sunshine.lnuplusweb.vo.superlesson.SuperLessonBean
import okhttp3.*
import java.io.IOException
import java.lang.Exception
import java.util.*
import kotlin.collections.ArrayList

/**
 * 导入超表类
 */
class LoadSuperTerm {

    fun login(user: String,
              password: String,
              loginCallback: LoginCallback<SuperData>){
        val okHttpClient = OkHttpClient()
            .newBuilder()
            .followRedirects(false)
            .build()
        val requestBody = FormBody.Builder()
            .add("updateInfo", "false")
            .add("phoneModel", "MI 8")
            .add("password", CryptoUtils.superEncrypt(password))
            .add("phoneBrand", "Xiaomi")
            .add("channel", "xiaomi")
            .add("platform", "1")
            .add("versionNumber", "9.5.9")
            .add("account", CryptoUtils.superEncrypt(user))
            .add("phoneVersion", "29")
            .build()
        val request = Request.Builder()
            .url("http://120.55.151.61/V2/StudentSkip/loginCheckV4.action")
            .post(requestBody)
            .build()
        okHttpClient.newCall(request).enqueue(object : Callback {
            override fun onFailure(call: Call, e: IOException) {
                loginCallback.onFail("没有网络连接")
            }

            override fun onResponse(call: Call, response: Response) {
                var cookie = ""
                response.headers.forEach {
                    if(it.first == "Set-Cookie" && it.second[0].equals('J')){
                        cookie = it.second
                    }
                }
                if(cookie.isEmpty()){
                    loginCallback.onFail("账号或密码错误")
                }else{
                    val superData = SuperData()
                    superData.cookie = cookie
                    try {
                        val jsonArray = JsonParser
                            .parseString(response.body?.string())
                            .asJsonObject
                            .getAsJsonObject("data")
                            .getAsJsonObject("student")
                            .getAsJsonObject("attachmentBO")
                            .getAsJsonArray("myTermList")

                        jsonArray.forEach {
                            var timeString = ""
                            try {
                                val jsonArray = JsonParser
                                    .parseString(it.toString())
                                    .asJsonObject
                                    .getAsJsonObject("courseTimeList")
                                    .getAsJsonArray("courseTimeBO")
                                jsonArray.forEach {time ->
                                    timeString +=
                                        time.asJsonObject.getAsJsonPrimitive("beginTimeStr").toString().substring(1, 3)+
                                                ":" +
                                                time.asJsonObject.getAsJsonPrimitive("beginTimeStr").toString().substring(3, 5) +
                                                "\n" +
                                                time.asJsonObject.getAsJsonPrimitive("endTimeStr").toString().substring(1, 3)+
                                                ":" +
                                                time.asJsonObject.getAsJsonPrimitive("endTimeStr").toString().substring(3, 5) +
                                                "|"
                                }
                                if(timeString[timeString.length - 1] == '|'){
                                    timeString = timeString.substring(0, timeString.length - 1)
                                }
                            }catch (e: Exception){

                            }

                            val superBean = SuperBean(
                                it.asJsonObject.getAsJsonPrimitive("beginYear").asString,
                                it.asJsonObject.getAsJsonPrimitive("maxCount").asInt,
                                it.asJsonObject.getAsJsonPrimitive("term").asString,
                                timeString
                            )
                            superData.superList.add(superBean)
                        }
                        //println("LoadSuperLesson 103->" + SuperData.superList.toTypedArray().contentToString())
                    }catch (e: Exception){
                        loginCallback.onFail("$e 很可能是账号或者密码错误噢")
                    }
                    loginCallback.onSuccess(superData)
                }
            }
        })
    }
}